﻿using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.ReduceFee;
using Common.Contract.Customer.Response.ReduceFee;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand.ReduceFee
{
    public class RetrieveCustomerReduceInfoByCifServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerReduceInfoRequest, RetrieveCustomerReduceInfoResponse>
    {
        #region Properties
        public string Cif;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        #endregion
        public RetrieveCustomerReduceInfoByCifServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerReduceInfoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Cif))
            {
                throw new ArgumentException("Cif Customer must be set", "request");
            }
            else
            {
                Cif = Request.Cif;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveCustomerReduceInfoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            CustomerReduceFeeInfoRecord record = new CustomerReduceFeeInfoRecord();
            Customers customer = new Customers();
            customer = Providers.Customers.RetrieveCustomerByCif(Cif);
            CustomerContact customerContact = Providers.Customers.RetrieveCustomerContactByCif(Cif);
            ProfitAbility toiAccumulated = Providers.Customers.RetrieveToiAccumulatedCustByCif(Cif);
            ProfitAbility toiAvgBal = Providers.Customers.RetrieveToiAvgBalAccumulatedCustByCif(Cif);
            Dictionary<string, Branch> listBranches = Providers.Branchs.RetrieveAllBranches().ToDictionary(x => x.BranchId);
            if (customerContact == null)
            {
                customerContact = new CustomerContact();
            }
            CustomerInfoRecord custInfo = new CustomerInfoRecord
            {
                Region = listBranches.ContainsKey(customer.BranchId) ? listBranches[customer.BranchId].Region : string.Empty,
                Branch = listBranches.ContainsKey(customer.BranchId) ? listBranches[customer.BranchId].BranchNameSme : string.Empty,
                Cif = customer.Cif,
                CustName = customer.CusName,
                Address = customer.Address,
                Email = customer.EmailAddr,
                Phone = customerContact.Mobile,
                LandLine = customerContact.LandLine,
                Office = customerContact.Office,
                Fax = customerContact.Fax,
                VbpIndustry = customer.IndustryName,
                Segment = customer.SubSegment,
                CustOpenDate = customer.CusOpenDate.HasValue ? customer.CusOpenDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture) : DateTime.MinValue.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
            };
            CustomerToiRecord toiInfo = new CustomerToiRecord
            {
                Month = DateTime.Now.AddMonths(-1),
                Month1 = 0,
                Month2 = 0,
                Month3 = 0,
                Month4 = 0,
                Month5 = 0,
                Month6 = 0,
                Month7 = 0,
                Month8 = 0,
                Month9 = 0,
                Month10 = 0,
                Month11 = 0,
                Month12 = 0,
            };
            CustomerToiRecord toiAvgbalInfo = new CustomerToiRecord
            {
                Month = DateTime.Now.AddMonths(-1),
                Month1 = 0,
                Month2 = 0,
                Month3 = 0,
                Month4 = 0,
                Month5 = 0,
                Month6 = 0,
                Month7 = 0,
                Month8 = 0,
                Month9 = 0,
                Month10 = 0,
                Month11 = 0,
                Month12 = 0,
            };
            if (toiAccumulated != null)
            {
                toiInfo = new CustomerToiRecord
                {
                    Month = toiAccumulated.Month,
                    Month1 = toiAccumulated.Month1,
                    Month2 = toiAccumulated.Month2,
                    Month3 = toiAccumulated.Month3,
                    Month4 = toiAccumulated.Month4,
                    Month5 = toiAccumulated.Month5,
                    Month6 = toiAccumulated.Month6,
                    Month7 = toiAccumulated.Month7,
                    Month8 = toiAccumulated.Month8,
                    Month9 = toiAccumulated.Month9,
                    Month10 = toiAccumulated.Month10,
                    Month11 = toiAccumulated.Month11,
                    Month12 = toiAccumulated.Month12,
                };
            }
            if (toiAvgBal != null)
            {
                toiAvgbalInfo = new CustomerToiRecord
                {
                    Month = toiAvgBal.Month,
                    Month1 = toiAvgBal.Month1,
                    Month2 = toiAvgBal.Month2,
                    Month3 = toiAvgBal.Month3,
                    Month4 = toiAvgBal.Month4,
                    Month5 = toiAvgBal.Month5,
                    Month6 = toiAvgBal.Month6,
                    Month7 = toiAvgBal.Month7,
                    Month8 = toiAvgBal.Month8,
                    Month9 = toiAvgBal.Month9,
                    Month10 = toiAvgBal.Month10,
                    Month11 = toiAvgBal.Month11,
                    Month12 = toiAvgBal.Month12,
                };
            }
            record.custToiInfo = toiInfo;
            record.custToiAvgInfo = toiAvgbalInfo;
            record.custInfo = custInfo;
            response.Record = record;

            return isSuccess;
        }
    }
}
